home *** CD-ROM | disk | FTP | other *** search
/ Aminet 34 / Aminet 34 (2000)(Schatztruhe)[!][Dec 1999].iso / Aminet / dev / basic / SocketFuncs.lha / v1.1 / TCPFuncs.bb < prev   
Encoding:
Text File  |  1999-10-10  |  9.1 KB  |  385 lines

  1. ; This code has been modified so that bsdsocket.library now
  2. ; no longer needs to be set up in Blitz.
  3. ;
  4. ; Modifications v1.1
  5. ;
  6. ; Modifications made:
  7. ;
  8. ; All bsdsocket.library function calls replaced with their ASM
  9. ; equivalent.
  10. ; TCPFuncs must now open and close the bsdsocket.library itself.
  11. ; This is done in ConnectTCP{} and CloseTCP{}.
  12. ;
  13. ; Modifications by Roger Light <rogerlight@mindless.com> all other
  14. ; work done as stated below.
  15. ;
  16. ; Thanks to Anton Reinauer for help with optimising the assembly code.
  17. ;
  18. ; If you have any problems with this code, I advised that you
  19. ; try the original TCPFuncs code to see if it is these modifications
  20. ; that are the problem. It may well be that these mods are at fault.
  21. ; Please don't contact me for support because I don't know much
  22. ; really and I will also have no access to an Amiga from the
  23. ; 11/10/99.
  24. ; If however you don't know what advantages this code gives you
  25. ; over the original TCPFuncs, but think you might need it, please
  26. ; feel free to contact me.
  27. ;
  28. ; Assemblified Socket Funcs:
  29.  
  30. Function.l socket{domain.l, type_.l, protocol.l}
  31. ;                   d0        d1        d2
  32.   MOVE.l a6,-(a7)
  33.   MOVE.l bsdsocketaddress,a6
  34.   JSR -30(a6)
  35.  
  36.   MOVE.l (a7)+,a6
  37.   AsmExit
  38. End Function
  39.  
  40. ;----
  41.  
  42. Function.l connect{s.l, *name.l, namelen.l}
  43. ;                   d0    a0        d1
  44.   MOVE.l a6,-(a7)
  45.   MOVE.l bsdsocketaddress,a6
  46.  
  47.   MOVE.l d1,a0
  48.   MOVE.l d2,d1
  49.  
  50.   JSR -54(a6)
  51.  
  52.   MOVE.l (a7)+,a6
  53.   AsmExit
  54. End Function
  55.  
  56. ;----
  57.  
  58. Function.l send{s.l, *buf.l, len_.l, flags.l}
  59. ;                d0    a0      d1      d2
  60.   MOVE.l a6,-(a7)
  61.   MOVE.l bsdsocketaddress,a6
  62.  
  63.   MOVE.l d1,a0
  64.   MOVE.l d2,d1
  65.   MOVE.l d3,d2
  66.  
  67.   JSR -66(a6)
  68.  
  69.   MOVE.l (a7)+,a6
  70.   AsmExit
  71.  
  72. End Function
  73.  
  74. ;----
  75.  
  76. Function.l recv{s.l, *buf.l, len_.l, flags.l}
  77. ;                d0    a0      d1      d2
  78.   MOVE.l a6,-(a7)
  79.   MOVE.l bsdsocketaddress,a6
  80.  
  81.   MOVE.l d1,a0
  82.   MOVE.l d2,d1
  83.   MOVE.l d3,d2
  84.  
  85.   JSR -78(a6)
  86.  
  87.   MOVE.l (a7)+,a6
  88.   AsmExit
  89.  
  90. End Function
  91.  
  92. ;----
  93.  
  94. Function.l IoctlSocket{s.l, req.l, *argp.l}
  95. ;                       d0    d1      a0
  96.   MOVE.l a6,-(a7)
  97.   MOVE.l bsdsocketaddress,a6
  98.  
  99.   MOVE.l d2,a0
  100.  
  101.   JSR -114(a6)
  102.  
  103.   MOVE.l (a7)+,a6
  104.   AsmExit
  105.  
  106. End Function
  107.  
  108. ;----
  109.  
  110. Statement CloseSocket{s.l}
  111. ;                       d0
  112.   MOVE.l a6,-(a7)
  113.   MOVE.l bsdsocketaddress,a6
  114.                         ; s.l in d0
  115.   JSR -120(a6)
  116.  
  117.   MOVE.l (a7)+,a6
  118.   AsmExit
  119.  
  120. End Statement
  121.  
  122. ;----
  123.  
  124. Function.l WaitSelect{nfds.l, *readfds.l, *writefds.l, *exceptfds.l, *timeout.l, *signal.l}
  125. ;                       d0        a0          a1            a2           a3         d1
  126.   MOVE.l a6,-(a7)
  127.   MOVE.l bsdsocketaddress,a6
  128.  
  129.   MOVE.l d1,a0
  130.   MOVE.l d2,a1
  131.   MOVE.l d3,a2
  132.   MOVE.l d4,a3
  133.   MOVE.l d5,d1
  134.  
  135.   JSR -126(a6)
  136.  
  137.   MOVE.l (a7)+,a6
  138.   AsmExit
  139. End Function
  140.  
  141. ;----
  142.  
  143. Function.l gethostbyname{*name.l}
  144. ;                           a0
  145.   MOVE.l a6,-(a7)
  146.   MOVE.l bsdsocketaddress,a6
  147.  
  148.   MOVE.l d0,a0
  149.  
  150.   JSR -210(a6)
  151.  
  152.   MOVE.l (a7)+,a6
  153.   AsmExit
  154.  
  155. End Function
  156.  
  157. ;---
  158.  
  159. ;-----------------------------------------------------------
  160. ; Standard Blitz TCP Functions V1.8 by Paul Burkey (c)1997-1998
  161. ; Compiled with help from Ercole Spiteri and Anton Reinauer
  162. ; Contact me at burkey@bigfoot.com
  163. ;-----------------------------------------------------------
  164. ;History
  165. ;-------
  166. ;<16.2.97> Version 1.8
  167. ;Added NLPrintTCP{} for easy send string with carrage return and newline.
  168. ;Removed need for 3rd Party libs (only bsdsocket.library needed)
  169. ;
  170. ;<24.12.97> Version 1.7
  171. ;ReadTCP{} Updated with extra safety and Speed
  172. ;
  173. ;<18.9.97> Version 1.6
  174. ;Added PrintTCP{}  for an easy "send string" command.
  175. ;Added NPrintTCP{} for easy send string with carrage return
  176. ;CheckTCP{} merged into the ConnectTCP{} function.
  177. ;
  178. ;---------------
  179. ; Function List
  180. ;---------------
  181. ;
  182. ;ReadTCP{}                       ; Similar to Edit$() - recives data via TCP connection
  183. ;ReadMemTCP{ReadAdd.l,MaxSize.l} ; Similar to ReamMem - recives data via TCP connection
  184. ;WriteTCP{ad.l,size.w}           ; Similar to WriteMem - sends data via TCP connection
  185. ;ConnectTCP{host$,port.w}        ; Connect to a remote machine (Full error checking)
  186. ;PrintTCP{text$}                 ; Similar to Print - sends data via TCP connection
  187. ;NPrintTCP{text$}                ; Similar to NPrint - sends data via TCP connection
  188. ;NLPrintTCP{text$}               ; Similar to Print+CR+LF - sends data via TCP connection
  189. ;CloseTCP{}                      ; Closes TCP Connection
  190.  
  191. ;---------------------------------
  192. ; TCP library Variables/Constants
  193. ;---------------------------------
  194.  
  195. #TCPBuflen=$2048                ;Maximum data size to read at any time
  196. TCPmem.l=AllocMem(#TCPBuflen,0) ;Allocate the temp buffer used for all TCP reads
  197. #FIONREAD=$4004667f             ;FIONREAD request
  198.  
  199. ;---------------------------------
  200. ; Standard TCP library structures
  201. ;---------------------------------
  202.  
  203. NEWTYPE.list
  204.   *ItemA.b
  205.   *ItemB.b
  206. End NEWTYPE
  207. NEWTYPE.inaddr
  208.   s_addr.l
  209. End NEWTYPE
  210. NEWTYPE.sockaddrin
  211.   sin_len.b
  212.   sin_family.b
  213.   sin_port.w
  214.   sin_addr.inaddr
  215.   sin_zero.b[8]
  216. End NEWTYPE
  217. NEWTYPE.hostent
  218.   *h_name.b
  219.   *h_aliases.list
  220.   h_addrtype.l
  221.   h_lenght.l
  222.   *h_addr_list.list
  223. End NEWTYPE
  224.  
  225. ;---------------------------------
  226. ; Standard TCP Blitz Functions
  227. ;---------------------------------
  228.  
  229. .ReadTCP
  230. Function .s ReadTCP{}
  231.   SHARED sock.l,TCPmem.l
  232.   ;
  233.   ; This Function reads data from the server the result is passed back in a
  234.   ; string. If there is no messages then it will return an empty string =""
  235.   ;
  236.   sockread.l=0                                ;Clear Readmask
  237.   sockread.l BitSet sock.l                    ;Set Readmask on our socket
  238.   e.l=IoctlSocket{sock.l,#FIONREAD,TCPmem.l}  ;How much data is there?
  239.   f.l=Peek.l(TCPmem.l)                        ;Place value in f
  240.   If f>0
  241.     If f>#TCPBuflen Then f=#TCPBuflen         ;Don't read more than #TCPBuflen
  242.     c=recv{sock.l,TCPmem.l,f,0}               ;Read all Data
  243.     c$=String$(" ",f)                         ;Reserve String
  244.     CopyMem_ TCPmem.l,&c$,f                   ;Copy Data to string
  245.     Function Return c$
  246.   Else
  247.     Function Return ""
  248.   EndIf
  249. End Function
  250.  
  251.  
  252. ;----
  253. ;WARNING: This is a 'rough' experiment function.
  254. ;Function will probably change next update.
  255. ;----
  256.  
  257. .ReadMemTCP
  258. Function .l ReadMemTCP{ReadAdd.l,MaxSize.l}
  259.   SHARED sock.l,TCPmem.l
  260.   ;
  261.   ; Read into memory location 'ReadAdd.l' up to a maximum of 'MaxSize.l'
  262.   ; Used for reading long binary files eg, WWW files or FTP files.
  263.   ; Also returns the amount of bytes actually read.
  264.   ;
  265.   sockread.l=0                                ;Clear Readmask
  266.   sockread.l BitSet sock.l                    ;Set Readmask on our socket
  267.   e.l=IoctlSocket{sock.l,#FIONREAD,TCPmem.l}  ;How much data is there?
  268.   f.l=Peek.l(TCPmem.l)                        ;Place value in f
  269.   If f>0
  270.     If f>#TCPBuflen Then f=#TCPBuflen         ;Don't read more than #TCPBuflen
  271.     If f>MaxSize Then f=MaxSize               ;Don't read more than MaxSize
  272.     c=recv{sock.l,ReadAdd.l,f,0}              ;Read Data to ReadAdd location
  273.     Function Return f
  274.   Else
  275.     Function Return 0
  276.   EndIf
  277.   ;
  278. End Function
  279.  
  280.  
  281.  
  282. .WriteMemTCP
  283. Statement WriteMemTCP{ad.l,size.w}
  284.   SHARED sock.l
  285.   ;
  286.   ; This routine writes data via TCP.
  287.   ;
  288.   sockwrite.l=0                           ;Clear Writemask
  289.   sockwrite.l BitSet sock.l               ;set Writemask on our socket
  290.   g.l=WaitSelect{2,0,&sockwrite.l,0,0,0}  ;Wait until server is ready to read our data
  291.   c.l=send{sock.l,ad,size,0}              ;Send data to server
  292. End Statement
  293.  
  294.  
  295.  
  296. .ConnectTCP
  297. Function .b ConnectTCP{host$,port.w}
  298.   SHARED sock.l
  299.   ;
  300.   ; Check if Miami/AmiTCP stack is available
  301.   ; Connect to host at specified port
  302.   ; Return true or False if Connection is made
  303.  
  304.   SocketBase.l=OpenLibrary_("bsdsocket.library",0)
  305.   If SocketBase=0
  306.     Function Return False
  307.   Else
  308.     GetReg a0,SocketBase.l        ; put SocketBase.l in a0
  309.     MOVE.l a0,bsdsocketaddress    ; store the bsdcoket.lib address
  310.  
  311. ;;    CloseLibrary_(SocketBase)
  312.     sock.l=socket{2,1,0}
  313.     *a.hostent=gethostbyname{host$}
  314.     If *a=0
  315.       Function Return False   ; host not found (or internal TCP error)
  316.     Else
  317.       ;
  318.       ; Copy Details to our Sockaddrin structure
  319.       ;
  320.       CopyMem_ *a\h_addr_list\ItemA,&host.sockaddrin\sin_addr,*a\h_lenght
  321.       host.sockaddrin\sin_port=port       ;Set port number
  322.       host.sockaddrin\sin_family=2        ;Set type to AT_INET
  323.       StructLength.l=SizeOf .sockaddrin   ;Get lenght of structure sockaddrin
  324.       If connect{sock.l,host.sockaddrin,StructLength}=-1
  325.         CloseSocket{sock.l}
  326.         Function Return False
  327.       Else
  328.         Function Return True
  329.       EndIf
  330.     EndIf
  331.   EndIf
  332. End Function
  333.  
  334.  
  335.  
  336. .PrintTCP
  337. Statement PrintTCP{text$}
  338.   ;
  339.   ; Send String via TCP
  340.   ;
  341.   WriteMemTCP{&text$,Len(text$)}
  342. End Statement
  343.  
  344.  
  345.  
  346. .NPrintTCP
  347. Statement NPrintTCP{text$}
  348.   ;
  349.   ; Send String via TCP + Carrage Return
  350.   ;
  351.   text$=text$+Chr$(13)
  352.   WriteMemTCP{&text$,Len(text$)}
  353. End Statement
  354.  
  355.  
  356.  
  357. .NLPrintTCP
  358. Statement NLPrintTCP{text$}
  359.   ;
  360.   ; Send String via TCP + Carrage Return + Line Feed
  361.   ;
  362.   text$=text$+Chr$(13)+Chr$(10)
  363.   WriteMemTCP{&text$,Len(text$)}
  364. End Statement
  365.  
  366.  
  367.  
  368. .CloseTCP
  369. Statement CloseTCP{}
  370.   SHARED sock.l
  371.   SHARED SocketBase.l
  372.   ;
  373.   ; This is a simple close socket command
  374.   ; Provided for the shear hell of it :)
  375.   ; Now also closes bsdsocket.library.
  376.   ;
  377.   CloseSocket{sock.l}
  378.   CloseLibrary_(SocketBase)
  379. End Statement
  380.  
  381.  
  382. bsdsocketaddress:
  383.   Dc.l 0        ;address stored here
  384.  
  385.